﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chenxin.Lib.Utility
{
    //****************************************************************
    //   臣信二手房
    //   Copyright (c) 2010 Chenxin.                        
    //****************************************************************
    //****************************************************************      
    //作成者       :  he.jifeng   
    //创建日期     :  2010-03-30                 
    //確認者       :  確認者          
    //功能概要　   :  字符首字母
    //****************************************************************
    public class InitialHelper
    {
        //*******************************************
        //功能概要       ：获得字符串首字母
        //参数説明       ：strText 字符串对象
        //返回值説明     ：字符串首字母
        //功能詳細       ：获得字符串首字母
        //作成者         ：he.jifeng
        //作成日         ：2010-03-30 
        //*******************************************
        static public string GetChineseSpell(string strText)
        {
            int len = strText.Length;
            string myStr = "";
            for (int i = 0; i < len; i++)
            {
                myStr += getInitial(strText.Substring(i, 1));
            }
            return myStr;
        }

        //*******************************************
        //功能概要       ：获得单个字符首字母
        //参数説明       ：cnChar 单个字符
        //返回值説明     ：单个字符首字母
        //功能詳細       ：获得单个字符首字母
        //作成者         ：he.jifeng
        //作成日         ：2010-03-30 
        //*******************************************
        static public string getInitial(string cnChar)
        {
            byte[] arrCN = Encoding.Default.GetBytes(cnChar);
            if (arrCN.Length > 1)
            {
                int area = (short)arrCN[0];
                int pos = (short)arrCN[1];
                int code = (area << 8) + pos;
                int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
                for (int i = 0; i < 26; i++)
                {
                    int max = 55290;
                    if (i != 25) max = areacode[i + 1];
                    if (areacode[i] <= code && code < max)
                    {
                        return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
                    }
                }
                return "*";
            }
            else return cnChar;
        }

        /// <summary>
        /// 小写转大写
        /// </summary>
        /// <param name="strScr"></param>
        /// <param name="strDst"></param>
        /// <returns></returns>
        public static bool TranslateNumberToChinese(string strScr, out string strDst)
        {
            const string strNumber = "1234567890.";
            const string strChineseNumber = "壹贰叁肆伍陆柒捌玖零";
            const string strChineseMoney = "元拾佰仟万拾佰仟亿拾佰仟";
            const string strChineseMoney2 = "角分";

            strDst = "";

            //检查输入字符是否有效
            for (int i = 0; i < strScr.Length; i++)
            {
                if (strNumber.IndexOf(strScr.Substring(i, 1)) < 0)
                {
                    return false;
                }
            }

            //检查“.”有没有超过两个
            int j = 0;
            for (int i = 0; i < strScr.Length; i++)
            {
                if (strScr.Substring(i, 1) == ".")
                {
                    j += 1;
                }
            }
            if (j > 1)
            {
                return false;
            }

            string strBefore;
            string strAfter;
            int nPos;

            nPos = strScr.IndexOf(".");
            if (nPos > 0)
            {
                strBefore = strScr.Substring(0, nPos);
                strAfter = strScr.Substring(nPos + 1);
            }
            else
            {
                strBefore = strScr;
                strAfter = "";
            }

            //去掉小数点后面的零
            if (strAfter.Length > 0)
            {
                for (int i = strAfter.Length - 1; i >= 0; i--)
                {
                    if (strAfter.Substring(i) == "0")
                    {
                        strAfter = strAfter.Substring(0, i);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (strAfter.Length > 2)
            {
                return false;
            }

            //去掉左边的0
            if (strBefore.Length > 0)
            {
                for (int i = strBefore.Length - 1; i >= 0; i--)
                {
                    if (strBefore.Substring(0, 1) == "0")
                    {
                        if (i >= 0)
                        {
                            strBefore = strBefore.Substring(1);
                        }
                        else
                        {
                            strBefore = "";
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }

            string ch;
            int n;
            //填充strBefore数字到strDst
            for (int i = 0; i < strBefore.Length; i++)
            {
                ch = strBefore.Substring(i, 1);
                n = Convert.ToInt32(ch);
                if (n == 0)
                {
                    ch = strDst.Substring(strDst.Length - 1, 1);
                    if (strBefore.Length - i - 1 == 0)
                    {
                        if (ch == "零")
                        {
                            strDst = strDst.Substring(0, strDst.Length - 1) + "元";
                        }
                        else
                        {
                            strDst += "元";
                        }
                    }
                    else if (strBefore.Length - i - 1 == 4)
                    {
                        if (ch == "零")
                        {
                            //							if (strBefore.Length > 8 && )
                            //							{
                            //								strDst = strDst.Substring(0, strDst.Length - 1);
                            //							}
                            //							else
                            //							{
                            strDst = strDst.Substring(0, strDst.Length - 1) + "万";
                            //							}
                        }
                        else
                        {
                            strDst += "万";
                        }
                    }
                    else if (strBefore.Length - i - 1 == 8)
                    {
                        if (ch == "零")
                        {
                            strDst = strDst.Substring(0, strDst.Length - 1) + "亿";
                        }
                        else
                        {
                            strDst += "亿";
                        }
                    }
                    else if (ch == "零")
                    {
                        //do nothing
                    }
                    else
                    {
                        strDst += "零";
                    }
                }
                else
                {
                    strDst += strChineseNumber.Substring(n - 1, 1);
                    strDst += strChineseMoney.Substring(strBefore.Length - i - 1, 1);
                }
            }

            //填充strAfter数字到strDst
            for (int i = 0; i < strAfter.Length; i++)
            {
                ch = strAfter.Substring(i, 1);
                n = Convert.ToInt32(ch);
                if (n != 0)
                {
                    strDst += strChineseNumber.Substring(n - 1, 1);
                    strDst += strChineseMoney2.Substring(i, 1);
                }
                else
                {
                    strDst += "零";
                }
            }

            if (strDst != "")
            {
                strDst += "整";
            }

            return true;
        }

    }
}
